package com.zzty.dao;

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.zzty.entity.UserAdmin;


public class UserDao {
    public UserAdmin getUserByName(String Name) throws SQLException {
		@SuppressWarnings("unused")
		String resource = "Configuration.xml";	
		
        Reader reader = null;	
        
        try {
			 reader =Resources.getResourceAsReader("Configuration.xml");	

		} catch (IOException e) {
			e.printStackTrace();
		}
        
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);	
        SqlSession session = sessionFactory.openSession();	
        String statement = "com.zzty.selectUserByUsername";
        UserAdmin user = new UserAdmin();
        user=session.selectOne(statement, Name); 
		return user;
		
	}

	public boolean addUser(UserAdmin user) {
		@SuppressWarnings("unused")
		List<UserAdmin> list = new ArrayList<UserAdmin>();
		String resource = "Configuration.xml";	
        Reader reader = null;	
        
        try {
			 reader =Resources.getResourceAsReader("Configuration.xml");	

		} catch (IOException e) {
			e.printStackTrace();
		}
        
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);	
        SqlSession session = sessionFactory.openSession();	
        String statement = "com.zzty.addUser";
		boolean flag = false;
			int count=session.insert(statement,user);
			
			if(count>0){
				flag=true;
			}
		session.commit();
		System.out.println(user);
		return flag;
	}

}
